/*
 * Created on 2006-4-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sitech.portal.system.action;

import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.system.domain.TblSYS_DEPT;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;
import com.sitech.portal.system.form.DepartForm;
import com.sitech.portal.system.service.ISysDeptService;
import com.sitech.portal.system.service.ISysLogInmsgService;
import com.sitech.portal.system.service.ISysRoleDeptService;
import com.sitech.portal.system.service.ISysRoleService;
import com.sitech.portal.system.service.ISysWorkGroupDeptService;
import com.sitech.portal.system.service.ISysWorkGroupService;


/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class DepartAction extends JGridBaseAction {

	private DepartForm deptForm;
	private ISysDeptService sysDeptService;
	private List mgremployee;
	private List roleList;
	private ISysLogInmsgService sysLogInmsgService;
	private ISysRoleDeptService sysRoleDeptService;
	private ISysRoleService sysRoleService;
	private ISysWorkGroupDeptService sysWorkGroupDeptService;
	private ISysWorkGroupService sysWorkGroupService;
	private List workGroupList;
	private String deptInfo;
	private String flag;
	
	public ISysWorkGroupService getSysWorkGroupService() {
		return sysWorkGroupService;
	}

	public void setSysWorkGroupService(ISysWorkGroupService sysWorkGroupService) {
		this.sysWorkGroupService = sysWorkGroupService;
	}

	public List getWorkGroupList() {
		return workGroupList;
	}

	public void setWorkGroupList(List workGroupList) {
		this.workGroupList = workGroupList;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	public ISysRoleService getSysRoleService() {
		return sysRoleService;
	}

	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}

	public ISysWorkGroupDeptService getSysWorkGroupDeptService() {
		return sysWorkGroupDeptService;
	}

	public void setSysWorkGroupDeptService(
			ISysWorkGroupDeptService sysWorkGroupDeptService) {
		this.sysWorkGroupDeptService = sysWorkGroupDeptService;
	}

	public ISysRoleDeptService getSysRoleDeptService() {
		return sysRoleDeptService;
	}

	public void setSysRoleDeptService(ISysRoleDeptService sysRoleDeptService) {
		this.sysRoleDeptService = sysRoleDeptService;
	}

	public DepartForm getDeptForm() {
		return deptForm;
	}

	public void setDeptForm(DepartForm deptForm) {
		this.deptForm = deptForm;
	}

	public ISysDeptService getSysDeptService() {
		return sysDeptService;
	}

	public void setSysDeptService(ISysDeptService sysDeptService) {
		this.sysDeptService = sysDeptService;
	}

	public List getMgremployee() {
		return mgremployee;
	}

	public void setMgremployee(List mgremployee) {
		this.mgremployee = mgremployee;
	}

	public ISysLogInmsgService getSysLogInmsgService() {
		return sysLogInmsgService;
	}

	public void setSysLogInmsgService(ISysLogInmsgService sysLogInmsgService) {
		this.sysLogInmsgService = sysLogInmsgService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String findDeptInfoByDeptId() {
		deptInfo = JSONUtil.toJSON(sysDeptService.findByDeptId(deptForm.getDeptId()));
		return SUCCESS;
	}

	public String loadDeptManage(){
        Boolean isAdmin =   sysRoleService.isAdmin(this.getCurrentUser().getLOGIN_ID());
        deptForm = new DepartForm();
        if(isAdmin){
            deptForm.setDeptName("部门管理");
            deptForm.setDeptId("0");
        }else{
            List<HashMap> list = sysDeptService.findDeptByMgrId(this.getCurrentUser().getLOGIN_ID());
            if(list!=null&&list.size()>0&&!isAdmin){
                HashMap map = list.get(0);
                deptForm.setDeptName(map.get("DEPT_NAME").toString());
                deptForm.setDeptId(map.get("DEPT_ID").toString());
            }else{
                deptForm.setDeptName("部门管理");
                deptForm.setDeptId("0");
            }
        }
		return SUCCESS;
	}
	
	public String loadDeptList(){
		String type = this.getRequest().getParameter("type");
		if(type!=null && type.equals("select"))
			return "SELECT";
		return SUCCESS;
	}
	
	public String loadDeptListQueryJson() {
		TblSYS_DEPT mObj = new TblSYS_DEPT();
		mObj.setSUPER_DEPT_ID(deptForm.getSUPER_DEPT_ID());
		IPage result = sysDeptService.findListByParentId(mObj, this.getPageForm());
		this.setResult(result);

		return SUCCESS;
	}
	                               

	/**
	 * 公司编辑页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String editDepart() {

		String deptId = deptForm.getDeptId();
		if (deptId != null) {

			TblSYS_DEPT obj = sysDeptService.findDepartByPK(deptId);
			deptForm.setDeptCode(obj.getDEPT_CODE());
			deptForm.setDeptDesc(obj.getDEPT_DESC());
			deptForm.setDeptName(obj.getDEPT_NAME());
			deptForm.setSUPER_DEPT_ID(obj.getSUPER_DEPT_ID());
			deptForm.setMgrId(obj.getMGR_ID());
			if(obj.getMGR_ID()!=null){//部门负责人
				TblSYS_LOGINMSG user = sysLogInmsgService.findByUserCode(obj.getMGR_ID());
				deptForm.setMgrName(user.getNAME());
				
			}
			roleList = sysRoleDeptService.findRoleDeptListByUser(this.getCurrentUser().getLOGIN_ID());
			if(roleList==null || roleList.size()==0){
				roleList = sysRoleService.findForListToAll();
			}
			
			workGroupList = sysWorkGroupDeptService.findWorkGroupDeptListByUser(this.getCurrentUser().getLOGIN_ID());
			if(workGroupList==null || workGroupList.size()==0){
				workGroupList = sysWorkGroupService.findAllWorkGroup();
			}
			
			String roleIds = "";
			List list = sysRoleDeptService.findRoleDeptListByDept(deptId);
			for(int i=0;i<list.size();i++){
				HashMap role = (HashMap)list.get(i);
				roleIds += role.get("ROLE_ID") + ",";
			}
			deptForm.setRoleIds(roleIds);
			
			String workGroupIds = "";
			List selectWorkGroupList = sysWorkGroupDeptService.findWorkGroupDeptListByDept(deptId);
			for(int i=0;i<selectWorkGroupList.size();i++){
				HashMap workGroup = (HashMap)selectWorkGroupList.get(i);
				workGroupIds += workGroup.get("WORKGROUP_ID") + ",";
			}
			deptForm.setWorkGroupIds(workGroupIds);
		}
		

		return SUCCESS;
	}

	/**
	 * 公司编辑页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String saveDepart() {

		flag = "true";
		try{
			String deptId = deptForm.getDeptId();
			TblSYS_DEPT d = new TblSYS_DEPT();
			d.setDEPT_ID(deptId);
			d.setDEPT_CODE(deptForm.getDeptCode());
			d.setDEPT_DESC(deptForm.getDeptDesc());
			d.setDEPT_NAME(deptForm.getDeptName());
			d.setSUPER_DEPT_ID(deptForm.getSUPER_DEPT_ID());
			if(deptForm.getMgrId()!=null && deptForm.getMgrId().length()>0){
				d.setMGR_ID(deptForm.getMgrId());
				String[] roles = this.getRequest().getParameterValues("selectRoles");
				if(roles!=null)
					sysRoleDeptService.updateRoleDept(deptId, deptForm.getMgrId(), roles);
				
				String[] workGroups = this.getRequest().getParameterValues("selectWorkGroups");
				if(workGroups!=null)
					sysWorkGroupDeptService.updateWorkGroupDept(deptId, deptForm.getMgrId(), workGroups);
			}
	
			if (deptForm.getISMP_JSP_COMMAND().equals("modify")) {
				sysDeptService.update(d);
			} else if (deptForm.getISMP_JSP_COMMAND().equals("add")) {
				sysDeptService.add(d);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

	/**
	 * 显示用户列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String loadListUser() {
		String deptType = "dept";
		String midId = this.getRequest().getParameter("deptId");
		IPage result = sysLogInmsgService.findListByDeparyType(deptType, midId, this.getPageForm());
		this.setResult(result);
		
		return SUCCESS;
	}

	/**
	 * 删除部门
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String delDepart() {
		flag = "true";
		String DEPT_ID = deptForm.getDeptId();
		
		try{
			List<HashMap> list = sysLogInmsgService.findUserByDepartId(DEPT_ID);
			if (list != null && list.size()>0) {
				flag = "该部门下有用户，请先删除用户信息";
				return SUCCESS;
			}
			TblSYS_DEPT c = new TblSYS_DEPT();
			c.setDEPT_ID(DEPT_ID);
			sysDeptService.delete(c);
		}catch(Exception e ){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

	public String getDeptInfo() {
		return deptInfo;
	}
}
